<!-- 补单扣款作业-3：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" />
    <div class="mt10" style="width: 100%; background-color: white">
      <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
        <h1 class="f16">补料责任归属查询</h1>
        <Table
          :summaryData="summaryData"
          summary
          selection
          :tableHeader="tableHeader"
          :tableData="tableData"
          @selectList="selectTableList"
          :isLoading="isLoading"
        />
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </el-card>
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
import fileRequest from '@/utils/fileRequest'
export default defineComponent({
  name: 'AdditionalDebitOperationFore'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '补料单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '补料部门',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateValue: '',
    dateLabel: '补料日期'
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '补料类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '部门',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '开单用户',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '责任归属',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '指令号',
    inputValue: ''
  },
  {
    dateType: 'month',
    type: 'dateMonth',
    dateLabel: '结算月份',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '结算方式',
    selectLabel: '',
    selectOptions: []
  },

  {
    type: 'checkbox',
    checkboxLabel: '未确认',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '已确认',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '本场部门',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '外包厂商',
    checked: true
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
]) as any

const formList2 = ref([
  {
    dateType: 'month',
    type: 'dateMonth',
    dateLabel: '结算月份',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '结算方式',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '财务备注',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录结算'
  },
  {
    type: 'button',
    checkboxLabel: '仅修改备注'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录取消结算'
  }
]) as any

// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '导出') {
    const res = (await fileRequest.post(`/mtlCosts/suppDuty/export`, {
      accountName: formList.value.find(
        (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算方式'
      )?.selectValue,
      blFlag: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料类型')
        ?.selectValue,
      deptNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '部门')
        ?.selectValue, //部门ID
      dutyDeptNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门')
        ?.selectValue, //补料部门ID
      dutyName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '责任归属')
        ?.selectValue,
      dutyType: cfmFlag2(), //1.本厂部门 2.外包厂商
      financeType: cfmFlag(), //1.未确认 2.已确认
      financeYymm: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
      )?.dateValue,
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令号'
      )?.inputValue,
      slipDateEnd: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '补料日期'
      )?.dateValue?.[1],
      slipDateStart: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '补料日期'
      )?.dateValue?.[0],
      slipNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '补料单号'
      )?.inputValue,
      sysUser: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '开单用户'
      )?.inputValue,
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const findClicked2 = async (content: string) => {
  if (content === '仅修改备注') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择需要打勾的数据')
      return
    }
    const res = (await LoginWork.put(
      `/mtlCosts/suppDutyTtlFinance?financeYymm=${formList2.value[0].dateValue}&accountName=${formList2.value[1].selectValue}&remark=${
        formList2.value[2].inputValue
      }&flag=${3}`,
      selectedLists.value.map((item: any) => {
        return { slipNo: item.slipNo, dutyName: item.dutyName }
      })
    )) as any
    if (res.code === 200) {
      JMessage.success('修改成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }

  if (content === '打勾记录结算') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择需要打勾的数据')
      return
    }
    const res = (await LoginWork.put(
      `/mtlCosts/suppDutyTtlFinance?financeYymm=${formList2.value[0].dateValue}&accountName=${formList2.value[1].selectValue}&remark=${
        formList2.value[2].inputValue
      }&flag=${1}`,
      selectedLists.value.map((item: any) => {
        return { slipNo: item.slipNo, dutyName: item.dutyName }
      })
    )) as any
    if (res.code === 200) {
      JMessage.success('打勾记录结算成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '打勾记录取消结算') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择需要打勾的数据')
      return
    }
    const res = (await LoginWork.put(
      `/mtlCosts/suppDutyTtlFinance?financeYymm=${formList2.value[0].dateValue}&accountName=${formList2.value[1].selectValue}&remark=${
        formList2.value[2].inputValue
      }&flag=${0}`,
      selectedLists.value
    )) as any
    if (res.code === 200) {
      JMessage.success('打勾记录结算成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
}
const tableData = ref([])
const imgList = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 8,
    num: 0
  },
  {
    index: 9,
    num: 0
  },
  {
    index: 10,
    num: 0
  },
  {
    index: 11,
    num: 0
  },
  {
    index: 12,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未确认')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已确认')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return 1
  } else if (Audited.checked) {
    return 2
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
const cfmFlag2 = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '本场部门')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '外包厂商')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return 1
  } else if (Audited.checked) {
    return 2
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/mtlCosts/suppDutyTtlQuery', {
    accountName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算方式')
      ?.selectValue,
    blFlag: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料类型')
      ?.selectValue,
    deptNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '部门')
      ?.selectValue, //部门ID
    dutyDeptNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门')
      ?.selectValue, //补料部门ID
    dutyName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '责任归属')
      ?.selectValue,
    dutyType: cfmFlag2(), //1.本厂部门 2.外包厂商
    financeType: cfmFlag(), //1.未确认 2.已确认
    financeYymm: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
    )?.dateValue,
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令号'
    )?.inputValue,
    slipDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '补料日期'
    )?.dateValue?.[1],
    slipDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '补料日期'
    )?.dateValue?.[0],
    slipNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '补料单号'
    )?.inputValue,
    sysUser: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '开单用户'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.dtos.map((item: any) => {
    if (item.financeYymm) {
      item.backgroundColor = '#80FF00'
    }
    return item
  })
  total.value = tableDataList.total
  currentRow()
  summaryData.value[0].num = tableDataList.sum.dutyQtySum
  summaryData.value[1].num = tableDataList.sum.dutyAmtSum
  summaryData.value[2].num = tableDataList.sum.freeAmtSum
  summaryData.value[3].num = tableDataList.sum.laborAmtSum
  summaryData.value[4].num = tableDataList.sum.amtSum
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const tableHeader = ref([
  {
    backgroundColor: true,
    width: '120px',
    label: '补料单号',
    prop: 'slipNo'
  },
  {
    label: '补料日期',
    prop: 'slipDate'
  },
  {
    label: '补料部门',
    prop: 'dutyDeptName'
  },
  {
    label: '指令号',
    prop: 'lotNo'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '责任归属',
    prop: 'dutyName'
  },
  {
    label: '部门',
    prop: 'deptName'
  },
  {
    label: '责任用量',
    prop: 'dutyQty'
  },
  {
    label: '责任金额',
    prop: 'dutyAmt'
  },
  {
    label: '免扣金额',
    prop: 'freeAmt'
  },
  {
    label: '人工费用',
    prop: 'laborAmt'
  },
  {
    label: '扣款金额',
    prop: 'amt'
  },
  {
    label: '结算方式',
    prop: 'accountName'
  },
  {
    label: '结算月份',
    prop: 'financeYymm'
  },
  {
    label: '财务备注',
    prop: 'remark'
  },
  {
    width: '120px',
    label: '结算单号',
    prop: 'balanceNo'
  }
])
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
  // // 给表二传指令号
  // let table2Data = await service.get<any, any>(`/pcFactorQuery/queryMtlAttribute/${lotNo}`)
  // tableData2.value = table2Data.slip
}

const getSelectList = async () => {
  let res = (await LoginWork.get<any, any>('/mtlCosts/suppDutyTtlFinBack')) as any
  if (res) {
    res.data.blFlag.forEach((item: any) => {
      formList.value[3].selectOptions?.push({
        optionKey: item,
        optionValue: item,
        optionLabel: item
      })
    })
    res.data.duty.forEach((item: any) => {
      formList.value[6].selectOptions?.push({
        optionKey: item,
        optionValue: item,
        optionLabel: item
      })
    })
    res.data.dept.forEach((item: any) => {
      formList.value[4].selectOptions?.push({
        optionKey: `${item.deptNo}`,
        optionValue: `${item.deptNo}`,
        optionLabel: `${item.deptNo}  ${item.deptName}`,
        LeftLabel: `${item.deptNo}`,
        RightLabel: `${item.deptName}`
      })
    })
  }

  const mtlDept = formList.value.find(
    (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门'
  ) as any
  const mtlDeps = await service.get<any, any>('/back/deptQuery')
  mtlDeps.forEach((Vendor: any) => {
    mtlDept.selectOptions.push({
      optionLabel: Vendor.deptName,
      optionValue: `${Vendor.deptNo}`,
      LeftLabel: `${Vendor.deptNo}`,
      RightLabel: `${Vendor.deptName}`
    })
  })

  formList2.value
    .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算方式')
    ?.selectOptions?.push(
      {
        optionKey: `荣阳大货`,
        optionLabel: `荣阳大货`,
        optionValue: `荣阳大货`
      },
      {
        optionKey: `荣信`,
        optionLabel: `荣信`,
        optionValue: `荣信`
      },
      {
        optionKey: `KINGLIKE`,
        optionLabel: `KINGLIKE`,
        optionValue: `KINGLIKE`
      }
    )
  formList.value
    .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算方式')
    ?.selectOptions?.push(
      {
        optionKey: `荣阳大货`,
        optionLabel: `荣阳大货`,
        optionValue: `荣阳大货`
      },
      {
        optionKey: `荣信`,
        optionLabel: `荣信`,
        optionValue: `荣信`
      },
      {
        optionKey: `KINGLIKE`,
        optionLabel: `KINGLIKE`,
        optionValue: `KINGLIKE`
      }
    )
}
onMounted(() => {
  getSelectList()
})

const selectedLists = ref([])
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}
</script>

<style lang="less" scoped></style>
